import {Controller, Ctx, Get, Params} from '../core/router';
import {Http} from "../core/http";
import * as pagination from 'pagination';
import {Context} from "koa";

@Controller('/product')
export default class ProductController {

    @Get()
    public async index(@Http() http, @Ctx() ctx: Context) {

        let currentPage = ctx.query.page ? ctx.query.page : 1;

        const body = await http.get('v1/good/search', {
            num: 10,
            page: currentPage
        });
        const data = body.data.data;
        const list = data.data;
        const totalResult = data.total;

        var paginator = new pagination.TemplatePaginator({
            prelink: ctx.request.path,
            current: currentPage,
            rowsPerPage: 10,
            totalResult: totalResult,
            template: function (result) {
                var i, len, prelink;
                var html = '<div><ul class="ui pagination pointing secondary menu">';
                if (result.pageCount < 2) {
                    html += '</ul></div>';
                    return html;
                }
                prelink = this.preparePreLink(result.prelink);
                if (result.previous) {
                    html += '<li class="page-item item"><a class="page-link" href="' + prelink + result.previous + '">' + this.options.translator('PREVIOUS') + '</a></li>';
                }
                if (result.range.length) {
                    for (i = 0, len = result.range.length; i < len; i++) {
                        if (result.range[i] === result.current) {
                            html += '<li class="active item page-item"><a class="page-link" href="' + prelink + result.range[i] + '">' + result.range[i] + '</a></li>';
                        } else {
                            html += '<li class="page-item item"><a class="page-link" href="' + prelink + result.range[i] + '">' + result.range[i] + '</a></li>';
                        }
                    }
                }
                if (result.next) {
                    html += '<li class="page-item item"><a class="page-link" href="' + prelink + result.next + '" class="paginator-next">' + this.options.translator('NEXT') + '</a></li>';
                }
                html += '</ul></div>';
                return html;
            }
        });

        var view_info = {
            products: list,
            currentPage,
            paginator
        };

        return ctx.render('product/index.swig', view_info);
    }

    @Get('info/:id')
    public async info(@Http() http, @Ctx() ctx, @Params() params) {
        const body = await http.get('v1/good/get', params);
        let data = body.data.data;

        const related = await http.get('v1/good/getKeywordGoods', {
            related_id: params.id
        });

        const related_data = related.data.data;
        const typeof_previous = typeof related_data.previous;
        const typeof_next = typeof related_data.next;
        data = {...data, ...related_data, typeof_previous, typeof_next};

        return ctx.render('product/info.swig', data);
    }

}
